package com.ahui.mallchat.common.common.utils;

import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;
import java.util.Optional;

/**
 * Author:ahui
 * Description:
 * Date: 2024/7/23 18:15
 */
public class SpELUtils {

    private static final ExpressionParser PARSER = new SpelExpressionParser();
    private static final DefaultParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer();
    public static String getMethodKey(Method method) {
        return method.getDeclaringClass() + "#" + method.getName();
    }

    public static String pareSpEl(Method method, Object[] args, String spEl) {
        String[] params = Optional.ofNullable(PARAMETER_NAME_DISCOVERER.getParameterNames(method)).orElse(new String[]{});
        StandardEvaluationContext context = new StandardEvaluationContext(); //el解析需要的上下文对象
        for (int i = 0; i < params.length; i++) {
            context.setVariable(params[i],args[i]);//所有参数都做为原材料扔进去
        }
        Expression expression = PARSER.parseExpression(spEl);
        return expression.getValue(context,String.class);
    }
}
